package com.sls.nzdapplication.ui.custom.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sls.nzdapplication.ui.custom.holder.ImageIconHolder;
import com.sls.nzdapplication.R;
import com.sls.nzdapplication.ui.widget.OnItemClickListener;
import com.sls.nzdapplication.ui.widget.OnItemLongClickListener;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ImageButtonAdapter extends RecyclerView.Adapter<ImageIconHolder>{

    private List<String> imagerList;
    private String type;

    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;

    public ImageButtonAdapter(List<String> imagerList,String type){
        this.imagerList = imagerList;
        this.type = type;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener){
        this.onItemLongClickListener = onItemLongClickListener;
    }

    @NonNull
    @Override
    public ImageIconHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(type.equals("equipment")){
            View itemView = ((LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.image_text_icon_view, parent, false);
            return new ImageIconHolder(itemView);
        }else{
            View itemView = ((LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.image_2_text_icon_view, parent, false);
            return new ImageIconHolder(itemView);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull ImageIconHolder holder, int position) {

        holder.textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener==null){
                    return;
                }
                onItemClickListener.onItemClick(view,position);
            }
        });

        holder.imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener==null){
                    return;
                }
                onItemClickListener.onItemClick(view,position);
            }
        });

        holder.imageView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                if(onItemLongClickListener !=null){
                    onItemLongClickListener.onItemLongClick(view,position);
                }
                return false;
            }
        });
    }

    @Override
    public int getItemCount() {
        if(imagerList == null){
            return 0;
        }
        return imagerList.size();
    }
}
